<?
/*
**2015-07-03 鼓浪屿轮渡
**VenueID 航班ID
*/

include('/var/www/html/new/d/module/api/glyld/glyld.class.php');
include('/var/www/html/new/d/module/api/function.class.php');

$PFT_infunction = new AllFunction();
$PFT_infunction->logfile('6|'.json_encode($_REQUEST),'get_data');

if (!class_exists("go_sql")){
	include("/var/www/html/new/conf/le.je");
	$le=new go_sql();
	$le->connect();
}

// $time1 = microtime(true);
$gly = new Ferry_gly();

//{"VenueID":"","RoundID":"","AreaID":"","SeatID":"","Tnum":"0","Fid":"7416","Ordern":"1960067","Action":"MOD"}
if(!isset($_REQUEST['Action'])){
	$tid = $_REQUEST['TicketID'] + 0; //门票id，根据门票id获取航线及票类
	$select_uuid = "select uuid,pid from uu_jq_ticket where id=$tid limit 1";
	$GLOBALS['le']->query($select_uuid);
	if(!$GLOBALS['le']->fetch_assoc())	exit('101');		
	$uuids = explode("_",$GLOBALS['le']->f('uuid'));  //查询票类id	
	$pid = $GLOBALS['le']->f('pid');  //查询票类id	
	
	$sellPrice_ldata = $PFT_infunction->Dynamic_Price_And_Storage('124185',$pid,substr($_REQUEST['PlayDate'],0,10),0,"27491");
	if($sellPrice_ldata == 105 || $sellPrice_ldata == 1065) exit('1065');
	$sellPrice_ldata_xml =simpleXML_load_string($sellPrice_ldata);
	$sellPrice = (string)$sellPrice_ldata_xml->Rec->UUsprice;
	$gprice = $sellPrice/100;	
	
	$ls_order = 'pft'.str_replace('.',mt_rand(1000,9999),microtime(true));
	$submit_array = array(
		'flightLineid'  => $uuids[0],							 // 航线ID
		'ticketid'      => $uuids[1],							 // 票类ID
		'dailyFlightGid'=> $_REQUEST['VenueID'],				 // 航班ID
		'date'          => substr($_REQUEST['PlayDate'],0,10),	 // 日期
		'Ordern'        => $ls_order, // 临时订单号
		'Tnum'          => $_REQUEST['Tnum'],					 // 数量
		'ordername'     => $_REQUEST['TouristName'],			 // 姓名
		'IDcar'			=> $_REQUEST['IdCard'],					 // 身份证
		'tel'			=> $_REQUEST['TouristTel'],				 // 手机号
		'gprice'		=> $gprice								 // 供货价
	);
	
	$submit_order = $gly->saveSaleOrder($submit_array);
	$submit_order_xml = simplexml_load_string($submit_order);
	if((int)$submit_order_xml->code == 0){
		$glyldOrder = (string)$submit_order_xml->content->saleOrder->sno;
		$PFT_infunction->logfile('6|'.$ls_order.'|'.$glyldOrder,'submitO_s'); //下单成功记录
		if($_REQUEST['PayStatus'] == 1){//支付订单	
			$update_order = $gly->payForSaleOrder($glyldOrder);
			$update_order_xml = simplexml_load_string($update_order);
			$log_pay_log = $submit_array['Ordern'].'|'.$glyldOrder.'|'.(int)$update_order_xml->code;
			if((int)$update_order_xml->code != 0){
				$PFT_infunction->logfile('6|'.$log_pay_log.'|'.(string)$update_order_xml->content,'orderPay_f'); //支付订单失败记录
			}else{
				$PFT_infunction->logfile('6|'.$log_pay_log,'orderPay_s'); //支付订单成功记录
			}
		}
		exit("200|0|0|0|".$ls_order."|".$glyldOrder);
	}else{
		$PFT_infunction->logfile('6|'.$ls_order."|".$_REQUEST['TouristTel']."|".(int)$submit_order_xml->code.'|'.(string)$submit_order_xml->content,'submitO_f'); //下单失败记录
		exit("101|0|0|0|0|");
	}	
}elseif($_REQUEST['Action'] == 'Relation'){ //下单成功
	$Ordern = $_REQUEST['Ordern'];
	$pft_order_xml = $PFT_infunction->Select_PFTorder($Ordern);
	$ls_order = (string)$pft_order_xml->Rec->UUvcode;
	$glyldOrder = (string)$pft_order_xml->Rec->UUtordernum;
	$tnum = (int)$pft_order_xml->Rec->UUtnum;
	
	$str="insert into all_api_order (pftOrder,apiOrder,tempOrder,coopB,oStatus,oStnum,apiCode,handleStatus,cTime) values('$Ordern','$glyldOrder','$ls_order',6,0,$tnum,'$glyldOrder',0,now())";
	$GLOBALS['le']->query($str);
}
elseif($_REQUEST['Action'] == 'MOD'){//取消订单
	if($_REQUEST['Tnum'] != 0) exit("1077|0|0|0|0|0");
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);
	$UUordernum =(string)$pft_order_xml->Rec->UUordernum;		
	$UUpaystatus =(int)$pft_order_xml->Rec->UUpaystatus;
	$ls_order = (string)$pft_order_xml->Rec->UUvcode;	
	$UUremotenum = (string)$pft_order_xml->Rec->UUtordernum;//第三方订单号

	if($UUpaystatus == 2)	$saveReturn = $gly->saveCancel($UUremotenum); //未支付订单取消
	else	$saveReturn = $gly->saveReturn($UUremotenum); //已支付订单取消
	
	$saveReturn_xml = simplexml_load_string($saveReturn);
	if((int)$saveReturn_xml->code == 0){	
	
		$str="update all_api_order set oStatus=3,handleStatus=0 where pftOrder='".$_REQUEST['Ordern']."' limit 1";
		$GLOBALS['le']->query($str);	
		
		$PFT_infunction->logfile('6|'.$UUordernum.'|'.$ls_order.'|'.$UUremotenum,'modify_s'); //取消订单成功
		if($UUpaystatus == 1){
			$poundageDescribe = $saveReturn_xml->content->saleOrder->poundageDescribe;//退款百分比
			$UUmid = (int)$pft_order_xml->Rec->UUmid; //分销商id
			$UUaid = (int)$pft_order_xml->Rec->UUaid; //供应商id
			$UUtprice = (float)$pft_order_xml->Rec->UUtprice; //结算价
			$UUtotalmoney = (float)$pft_order_xml->Rec->UUtotalmoney; 
			$Debit = (ceil(($UUtprice / $poundageDescribe)/100))*100;//扣款金额
			$modify_money = $PFT_infunction->PFT_Modify_Money($UUmid,$Debit,1,0,0,0,$_REQUEST['Ordern']);
		}
		exit("200|0|0|0");
	}
	else{
		$PFT_infunction->logfile('6|'.$UUordernum.'|'.$ls_order.'|'.$UUremotenum.'|'.(string)$saveReturn_xml->code."|".(string)$saveReturn_xml->content,'modify_f'); //取消订单失败
		exit("1077");
	}
}elseif($_REQUEST['Action'] == 'Relation_after_pay'){ //订单支付后通知
	$pft_order_xml = $PFT_infunction->Select_PFTorder($_REQUEST['Ordern']);	
	$UUordernum =(string)$pft_order_xml->Rec->UUordernum;	
	$UUremotenum = (string)$pft_order_xml->Rec->UUtordernum;	
	$update_order = $gly->payForSaleOrder($UUremotenum);
	$update_order_xml = simplexml_load_string($update_order);
	$log_pay_log = $UUordernum.'|'.(string)$pft_order_xml->Rec->UUtordernum.'|'.(int)$update_order_xml->code;
	if((int)$update_order_xml->code != 0){
		$PFT_infunction->logfile('6|'.$log_pay_log.'|'.(string)$update_order_xml->content,'orderPay_f'); //支付订单失败记录
	}else{
		$PFT_infunction->logfile('6|'.$log_pay_log,'orderPay_s'); //支付订单成功记录
	}
}
?>